package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.mapper.CourseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 11839
* @description 针对表【sys_course】的数据库操作Service实现
* @createDate 2024-06-28 15:23:44
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{


    @Resource
    private CourseMapper courseMapper;
    @Override
    public ResponseResult coursePage(QueryCondition<Course> queryConditione) {
        Page<Course> coursePage = new Page<>(queryConditione.getPage(), queryConditione.getLimit());

        String courseName = queryConditione.getSearchParams().getCourseName();
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.eq(StringUtils.isNotBlank(courseName),Course::getCourseName,courseName)
                .orderByDesc(Course::getCreateTime);

        baseMapper.selectPage(coursePage,lambda);

        long total = coursePage.getTotal();


        return ResponseResult.success().data("total",total).data("tableData",coursePage.getRecords());
    }

    @Override
    public ResponseResult savaOrUpdateCourse(Course course) {
        //传进来一个数据  先看是新增还是修改
         if (course.getId() == null) {
             //新增
             String courseName = course.getCourseName();
             String courseNo = course.getCourseNo();

             LambdaQueryWrapper<Course> eq = new QueryWrapper<Course>().lambda()
                     .eq(StringUtils.isNotBlank(courseName), Course::getCourseName, courseName)
                     .or().eq(StringUtils.isNotBlank(courseNo), Course::getCourseNo, courseNo);
             Course course2 = baseMapper.selectOne(eq);
             if (course2!=null){
                 return ResponseResult.fail().message("添加的数据已存在");
             }
             Course course1=courseMapper.select(courseName,courseNo);
             if(course1!=null){
                 course1.setIsDelete(0);
                 courseMapper.idUpdate(course1.getCourseName(),course1.getCourseNo(),course1.getId());
                 return ResponseResult.success().message("插入成功");
             }
             baseMapper.insert(course);
             return ResponseResult.success().message("插入成功");


         }else {
             //修改
             int i = baseMapper.updateById(course);
             if (i==0){
                 return ResponseResult.fail().message("请刷新后重试");
             }
             return ResponseResult.success().message("编辑成功");
         }
    }

    @Override
    public List<Course> selectAllIncludingDeleted() {
        return courseMapper.selectAllIncludingDeleted();
    }
}




